package com.zbkj.client.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zbkj.client.config.WebSocketSessionManager;
import com.zbkj.client.service.AgentCommonService;
import com.zbkj.common.exception.CrmebException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class AgentCommonServiceImpl implements AgentCommonService {

    @Value("${workbench.agent}")
    private String agentBaseUrl;

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private WebSocketSessionManager webSocketSessionManager;

    private final String agentListUrl = "/api/v1/agents/";
    private final String groupListUrl = "/api/v1/groups/";

    /**
     * 热榜列表
     */
    @Override
    public List<Map<String, Object>> getAgentList(){
        String url = agentBaseUrl + agentListUrl;

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(headers);
        ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class);
        Map<String, Object> respBody = response.getBody();

        if (respBody == null) {
            throw new CrmebException("agent接口响应为空");
        }
        Integer codeObj = (Integer) respBody.get("code");

        if (codeObj != null && Integer.valueOf(200).equals(codeObj)) {
            List<Map<String, Object>> data = (List<Map<String, Object>> ) respBody.get("data");
            return  data;
        }

        return null;
    }

    /**
     * 获取代理详细信息
     */
    @Override
    public Map<String, Object> getAgentInfo(String agentId){
        String url = agentBaseUrl + agentListUrl + agentId;

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(headers);
        ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class);
        Map<String, Object> respBody = response.getBody();

        if (respBody == null) {
            throw new CrmebException("agent接口响应为空");
        }
        Integer codeObj = (Integer) respBody.get("code");

        if (codeObj != null && Integer.valueOf(200).equals(codeObj)) {
            Map<String, Object> data = (Map<String, Object>) respBody.get("data");
            return  data;
        }

        return null;
    }


    /**
     * 热榜列表
     */
    @Override
    public List<Map<String, Object>> getGroupList(){
        String url = agentBaseUrl + groupListUrl;

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(headers);
        ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class);
        Map<String, Object> respBody = response.getBody();

        if (respBody == null) {
            throw new CrmebException("群组接口响应为空");
        }
        Integer codeObj = (Integer) respBody.get("code");

        if (codeObj != null && Integer.valueOf(200).equals(codeObj)) {
            Map<String, Object> data = (Map<String, Object>) respBody.get("data");
            if (data != null && !data.isEmpty()) {
                List<Map<String, Object>> groups = (List<Map<String, Object>>)data.get("groups");
                return  groups;
            }
        }

        return null;
    }

    /**
     * 获取代理详细信息
     */
    @Override
    public Map<String, Object> getGroupInfo(String groupId){

        //包含成员信息
        String url = agentBaseUrl + groupListUrl + groupId + "?include_members=true";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(headers);
        ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class);
        Map<String, Object> respBody = response.getBody();

        if (respBody == null) {
            throw new CrmebException("agent接口响应为空");
        }
        Integer codeObj = (Integer) respBody.get("code");

        if (codeObj != null && Integer.valueOf(200).equals(codeObj)) {
            Map<String, Object> data = (Map<String, Object>) respBody.get("data");
            return  data;
        }

        return null;
    }

} 